#include "stdafx.h"
#include "GdiplusEx.h"

using namespace Gdiplus;

GdiplusObject::GdiplusObject()
{
	GdiplusStartupInput input;
	GdiplusStartup(&m_ulToken, &input, nullptr);
}
GdiplusObject::~GdiplusObject()
{
	GdiplusShutdown(m_ulToken);
}

GpsFontEx::GpsFontEx(const FontFormat & format)
	: GpsFont(format.FaceName, format.FontSize, 0, UnitPixel)
{

}
GpsFontEx::GpsFontEx(const char_16 * szFace, float_32 fSize)
	: GpsFont(szFace, fSize, 0, UnitPixel)
{

}

GpsPenEx::GpsPenEx(IPen * pPen)
	: GpsPen(pPen->DoSample(), pPen->GetWidth())
{
}
GpsPenEx::GpsPenEx(uint_32 uiColor, float_32 fWidth)
	: GpsPen(uiColor, fWidth)
{
}

GpsSolidBrushEx::GpsSolidBrushEx(IBrush * pBrush)
	: GpsSolidBrush(pBrush->DoSample())
{

}

GpsSolidBrushEx::GpsSolidBrushEx(uint_32 uiColor)
: GpsSolidBrush(uiColor)
{

}











const char_16 FMT_STRING[][11] = {L"image/bmp", L"image/jpeg", L"image/png", L"image/gif", L"image/tiff"};

BOOL GetEncoderClsid(ImageSaveFormat format, CLSID & clsid)
{
	const char_16 * pFormat = NULL;
	switch(format)
	{
	case ImageSaveFormatBmp:
		pFormat = FMT_STRING[0];
		break;
	case ImageSaveFormatJpeg:
		pFormat = FMT_STRING[1];
		break;
	case ImageSaveFormatPng:
		pFormat = FMT_STRING[2];
		break;
	case ImageSaveFormatGif:
		pFormat = FMT_STRING[3];
		break;
	case ImageSaveFormatTiff:
		pFormat = FMT_STRING[4];
		break;
	default:
		clsid = CLSID_NULL;
		return FALSE;
	}

	UINT_32 num = 0, size = 0;
	GetImageEncodersSize((UINT *)&num, (UINT *)&size);
	ImageCodecInfo * pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
	GetImageEncoders(num, size, pImageCodecInfo);

	for (uint_32 cnt = 0; cnt != num; ++cnt)
	{
		const char_16 * pFormatX = pImageCodecInfo[cnt].MimeType;
		if (0 == wcscmp(pImageCodecInfo[cnt].MimeType, pFormat))
		{
			clsid = pImageCodecInfo[cnt].Clsid;

			free(pImageCodecInfo);
			return TRUE;
		}
	}
	free(pImageCodecInfo);
	return FALSE;
}

BOOL SaveImageToFile(HBITMAP hBitmap, LPCWSTR szFileName, ImageSaveFormat format)
{
	CLSID encoder;
	if(GetEncoderClsid(format, encoder))
	{
		GpsImage * pImage = Bitmap::FromHBITMAP(hBitmap, NULL);
		return pImage->Save(szFileName, &encoder) == Ok;
	}
	return FALSE;
}
BOOL SaveImageToFile(GpsImage & image, LPCWSTR szFileName, ImageSaveFormat format)
{
	CLSID encoder;
	if(GetEncoderClsid(format, encoder))
	{
		return image.Save(szFileName, &encoder) == Ok;
	}
	return FALSE;
}
Image * ImageFromResource(UINT nID, LPCWSTR szType)
{
	Image * pImage = NULL;
	HRSRC hRsrc = ::FindResourceW(NULL, (LPCWSTR)nID, szType);
	if(hRsrc)
	{
		HGLOBAL hRes= ::LoadResource(NULL, hRsrc);
		if(hRes)
		{
			VOID * pTheImage= ::LockResource(hRes);
			if(pTheImage)
			{ 
				DWORD dwTheImage = ::SizeofResource(NULL, hRsrc);
				ULONG uWrite(0);
				IStream * pStream = NULL;
				CreateStreamOnHGlobal(NULL, TRUE, &pStream);
				pStream->Write(pTheImage, dwTheImage, &uWrite);
				pImage = Image::FromStream(pStream);
				pStream->Release();
				UnlockResource(hRes);
			} 
			FreeResource(hRes);
		}
	}
	return pImage;
}

